package com.pi.pro.xcmg.am.rf.web.cust.controller;


import com.fasterxml.jackson.annotation.JsonView;
import com.pi.common.utils.StringUtils;
import com.pi.framework.core.bean.PageResult;
import com.pi.framework.core.bean.Result;
import com.pi.framework.core.service.ICrudService;
import com.pi.pro.xcmg.am.was.edi.wcs.common.WcsWasXcmgExchangeConstants;
import com.pi.pro.xcmg.am.was.edi.wcs.dto.EPalletRequestData;
import com.pi.pro.xcmg.am.was.fac.service.WasCustomWorkService;
import com.pi.pro.xcmg.am.was.fac.service.WcsCustomWorkApiService;
import com.pi.pro.xcmg.am.was.fac.service.WcsCustomWorkService;
import com.pi.wms.dto.act.TaskActionDto;
import com.pi.wms.entity.TwaInventoryDetails;
import com.pi.wms.rf.api.controller.BaseApiController;
import com.pi.wms.service.DivisionService;
import com.pi.wms.service.InventoryDetailsService;
import com.pi.wms.service.StorageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



@Slf4j
@Api(value = "空托盘作业")
@RestController
@RequestMapping("/inventory/epal")
public class InventoryEpalApiController extends BaseApiController<TwaInventoryDetails, Long> {

    @Autowired
    private InventoryDetailsService inventoryService;

    @Autowired
    private DivisionService divisionService;

    @Autowired
    private WasCustomWorkService wasCustomWorkService;

    @Autowired
    private WcsCustomWorkApiService wcsCustomWorkService;

    @Autowired
    private StorageService storageService;

	@Override
	protected ICrudService<TwaInventoryDetails, Long> getCrudService() {
		return this.inventoryService;
	}


    @JsonView(value = { PageResult.View.class })
    @ApiOperation("接收空托盘，用于上架存储")
    @GetMapping("/accept")
    public Result receive(@ApiParam(name = "containernumber")  @RequestParam(name = "containernumber", required=true) String containernumber,
                                     @ApiParam(name = "locationcode")  @RequestParam(name = "locationcode", required=false) String locationcode,
                                     @ApiParam(name = "warehousecode") @RequestParam(name = "warehousecode", required=false) String warehousecode,
                                     @ApiParam(name = "containertype") @RequestParam(name = "containertype", required=false) String containertype,
                                     @ApiParam(name = "workmode") @RequestParam(name = "workmode", required=false) String workmode,
                                     @ApiParam(name = "usercode") @RequestParam(name = "usercode", required=false) String usercode) {

        log.debug("Controller InventoryEpalApiController accept()//containernumber=%s, loccode=%s, warehousecode=%s, containertype=%s, workmode=%s,  usercode=%s", containernumber, locationcode, warehousecode, containertype, workmode,  usercode);
        if (StringUtils.isNullOrBlank(containernumber)){
            return resultError(501, "请扫描托盘码");
        }
        String m=this.wasCustomWorkService.acceptEpal(containernumber, warehousecode,containertype,locationcode, workmode);
        if (!StringUtils.isNullOrBlank(m)){
            return resultError(501, "空托盘接收失败:"+m);
        }
        return resultOk("空托盘接收成功");
    }


    @JsonView(value = { PageResult.View.class })
    @ApiOperation("释放空托盘")
    @GetMapping("/release")
    public Result release(@ApiParam(name = "containernumber")  @RequestParam(name = "containernumber", required=true) String containernumber,
                                     @ApiParam(name = "locationcode")  @RequestParam(name = "locationcode", required=false) String locationcode,
                                     @ApiParam(name = "warehousecode") @RequestParam(name = "warehousecode", required=false) String warehousecode,
                                     @ApiParam(name = "workmode") @RequestParam(name = "workmode", required=false) String workmode,
                                     @ApiParam(name = "usercode") @RequestParam(name = "usercode", required=false) String usercode) {

        log.debug("Controller InventoryEpalApiController release()//containernumber=%s, loccode=%s, warehousecode=%s, workmode=%s,  usercode=%s", containernumber, locationcode, warehousecode, workmode,  usercode);
        if (StringUtils.isNullOrBlank(containernumber)){
            return resultError(501, "请扫描托盘码");
        }
        String m=this.wasCustomWorkService.releaseEpal(containernumber);
        if (!StringUtils.isNullOrBlank(m)){
            return resultError(501, "空托盘释放失败:"+m);
        }
        return resultOk("空托盘释放成功");
    }

    @JsonView(value = { PageResult.View.class })
    @ApiOperation("呼叫空托盘")
    @GetMapping("/replenish")
    public Result replenish(@ApiParam(name = "containertype")  @RequestParam(name = "containertype", required=true) String containertype,
                          @ApiParam(name = "tolocationcode")  @RequestParam(name = "tolocationcode", required=true) String tolocationcode,
                          @ApiParam(name = "count")  @RequestParam(name = "count", required=true) Integer count,
                          @ApiParam(name = "warehousecode") @RequestParam(name = "warehousecode", required=false) String warehousecode,
                          @ApiParam(name = "workmode") @RequestParam(name = "workmode", required=false) String workmode,
                          @ApiParam(name = "usercode") @RequestParam(name = "usercode", required=false) String usercode) {

        log.debug("Controller InventoryEpalApiController call()//containertype=%s, tolocationcode=%s, count=%s, warehousecode=%s, workmode=%s,  usercode=%s", containertype, tolocationcode, count, warehousecode, workmode,  usercode);
        if (StringUtils.isNullOrBlank(containertype)){
            return resultError(501, "请指定托盘类型");
        }
        if (StringUtils.isNullOrBlank(tolocationcode)){
            return resultError(501, "请指定呼叫站台");
        }
        EPalletRequestData requestData=new EPalletRequestData();
        requestData.setType(WcsWasXcmgExchangeConstants.CONTAINER_TYPE_TOTE);
        requestData.setCode(tolocationcode.trim());
        if (!StringUtils.isNullOrBlank(containertype)){
            requestData.setType(containertype);
        }
        requestData.setQuantity(1);
        if ((count>0) && (count<10)){
            requestData.setQuantity(count);
        }
        TaskActionDto actionDto=new TaskActionDto();
        String m=this.wcsCustomWorkService.epalReplenish(requestData, actionDto);
        if (!StringUtils.isNullOrBlank(m)){
            return resultError(501, "呼叫空托失败:"+m);
        }
        return resultOk("呼叫空托成功");
    }

}
